!function VueProductSelector(){

    var vm_data = {
        banner_name : '',       //banner名称
        bannerable_type : 'Glass',  //选中的banner类型
        banners : [],       //所有的banner
        all_products : {},    //所有的商品
        banner_image : ''       //banner图
    };

    var type_map = {
        Glass : '镜框',
        Lens : '镜片',
        BeautyContactLens : '彩色隐形眼镜',
        TransparentContactLens : '透明隐形眼镜',
        NursingSupply : '护理用品'
    };

    // bind type select change event to vue
    $('.product_type_select').on('change',function(){
        vm_data.bannerable_type = $(this).val();
        setTimeout(function(){
            $('.product-select-wrapper').find('select').select2();
        },50);
    });

    var vm = new Vue({

        el : '#v_app',

        data : vm_data,

        created : function (){

            this.initData();

        },
        methods : {

            //修改banner
            reviseBanner : function reviseBanner(id){

                var current_banner = this.banners.filter(function(item){

                    return item.id == id;

                })[0];

                this.banner_name = current_banner.name;

                $('#editor_form .product_type_select').select2('data',
                    {
                        'id':current_banner.bannerable_type,
                        'text':type_map[current_banner.bannerable_type]
                    });

                $('#editor_form .selected_product').select2('data',
                    {
                        'id':current_banner.bannerable.id,
                        'text':current_banner.bannerable.first_title
                    });

                this.banner_image = current_banner.image;

            },

            //初始化数据
            initData : function initData() {

                this.$http
                    .get(
                        BANNER_LIST
                    )
                    .then(function(res){

                        this.banners = res.data.banners;

                        this.all_products = res.data.all_products;

                        //给select2设置初始值
                        $('#appset-banner-form .selected_product').select2('data',
                            {
                                'id':this.all_products.glasses[0].id,
                                'text':this.all_products.glasses[0].first_title
                            });

                    })
                    .catch(function (err) {

                        alert('请求失败！');
                        console.log(err);

                    })

            }
        }
    });

}();